<template>
	<view>
		<yk-header :title="title" @headBtn="headBtn()" @closePage="closePage()"></yk-header>
		<view class="switch-box">
			客户申请领券，开启自动发券
			<switch class="switch" :checked="openPer==1?true:false"  @change="openSendcard()" />
		</view>
		<view class="send-list">
			<view class="send-head"><image src="../../static/img/zhanye/guest-icon.png" mode=""></image>自动发券记录</view>
			<view class="send-box" 
				v-for="(customer, index) in SendcardList"
				:key="index"
			>
				<view>客户{{customer.remarks_name}}自动发券{{customer.alteration_detailed}}元</view>
				<view>{{customer.create_time}}</view>
			</view>
			<!-- <view class="send-box">
				<view>客户13600001234自动发券200元</view>
				<view>9月21日 15:29</view>
			</view>
			<view class="send-box">
				<view>客户13600001234自动发券200元</view>
				<view>9月21日 15:29</view>
			</view>
			<view class="send-box">
				<view>客户13600001234自动发券200元</view>
				<view>9月21日 15:29</view>
			</view> -->
		</view>
		
		<view class="loading-text" v-if="SendcardList.length>10||SendcardList.length<1">{{ loadingText }}</view>
	</view>
</template>

<script>
	let page = 1,
		limit = 10;
	export default {
		data() {
			return {
				title: '客勤小助手',
				SendcardList:[],
				loadingText:'',
				openPer:''
			};
		},
		onLoad() {
	
		},
		onShow() {
			document.title = '客勤小助手';
			page = 1;
			limit = 10;
			this.SendcardList = [];
			this.loadingText = '加载中...';
			this.getSendcardList();
			this.getAutosend();
		},
		methods: {
			//顶部功能键
			headBtn(e) {
				let that = this;
				uni.showActionSheet({
					itemList: ['刷新', '清除缓存'],
					success: function(res) {
						console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						if (res.tapIndex == 0) {
							//刷新
							uni.showLoading({
								title: '刷新中'
							});
							setTimeout(function() {
								uni.hideLoading();
							}, 1500);
	
						} else {
							//清楚缓存
							// callNative('onClearH5Cache')
							uni.showToast({
								title: '清除缓存成功'
							});
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			},
			//关闭当前页面
			closePage() {
				callNative('navigateBack')
			},
			//开启、关闭自动发券
			openSendcard(){
				try {
					// console.log('getbindcardList === ' + uni.getStorageSync('token'));
					uni.request({
						url: this.$usereq + '/coupon-expand/customer/quotatotal/automatic/voucher',
						method: 'POST',
						header: {
							'blade-auth': decodeURI(uni.getStorageSync('token')),
							'content-type': 'application/json'
						},
						success: res => {
							let datas = res.data;
							console.log(datas);
							if(datas.code==200){
								uni.showToast({
									title: datas.data.msg,
									icon:'none',
									duration: 2000
								});
							}else{
								uni.showToast({
									icon:'none', 
									title: datas.msg,
									duration: 2000
								});
							}
							uni.stopPullDownRefresh();
						}
					});
				} catch (e) {
					uni.hideLoading();
					console.log(e.message);
				}
			},
			//自动发券列表
			getSendcardList() {
				try {
					// console.log('getbindcardList === ' + uni.getStorageSync('token'));
					uni.request({
						url: this.$usereq + '/coupon-expand/customer/quotatotal/quota/list',
						method: 'GET',
						header: {
							'blade-auth': decodeURI(uni.getStorageSync('token')),
							'content-type': 'application/json'
						},
						data: {
							page: page,
							limit: limit,
							detailedStatus:2
						},
						success: res => {
							let datas = res.data;
							console.log(datas);
							let newList = datas.data;
							if (newList != null && newList != '' && newList.length > 0) {
								this.SendcardList = this.SendcardList.concat(newList);
								page++;
								if (newList.length <= 9) {
									this.loadingText = '没有更多了';
								}
							} else {
								this.loadingText = '没有更多了';
							}
							uni.stopPullDownRefresh();
						}
					});
				} catch (e) {
					uni.hideLoading();
					this.loadingText = '没有更多了';
					console.log(e.message);
				}
			},
			//获取自动发券状态
			getAutosend() {
				try {
					// console.log('getbindcardList === ' + uni.getStorageSync('token'));
					uni.request({
						url: this.$usereq + '/coupon-expand/customer/quotatotal/automatic/status',
						method: 'GET',
						header: {
							'blade-auth': decodeURI(uni.getStorageSync('token')),
							'content-type': 'application/json'
						},
						success: res => {
							let datas = res.data;
							console.log(datas);
							if(datas.code==200){
								this.openPer=datas.data.openPer;
								console.log(this.openPer);
							}
							uni.stopPullDownRefresh();
						}
					});
				} catch (e) {
					uni.hideLoading();
					console.log(e.message);
				}
			}
		}
	};
</script>

<style lang="scss">
	page{
		background: #f2f2f2;
	}
	.switch-box{
		background: #fff;
		border-top: 1px solid #f2f2f2;
		height: 140px;
		line-height: 140px;
		padding: 0 30px;
		box-sizing: border-box;
		font-size: 32px;
		.switch{
			float: right;
		}
	}
	.send-list{
		margin-top: 50px;
		padding: 0 30px;
		box-sizing: border-box;
		.send-head{
			line-height: 40px;
			height: 40px;
			font-size: 28px;
			image{
				float: left;
				margin-right: 5px;
				margin-top: 8px;
				width: 22px;
				height: 24px;
			}
		}
		.send-box{
			background: #fff;
			border-radius: 10px;
			padding: 35px 30px;
			margin-top: 20px;
			view:nth-child(1){
				font-size:30px;
				font-family:PingFang SC;
				font-weight:500;
				color:rgba(31,31,31,1);
				line-height:32px;
			}
			view:nth-child(2){
				font-size:24px;
				font-family:PingFang SC;
				font-weight:500;
				color:rgba(137,137,137,1);
				line-height:32px;
				margin-top: 20px;
			}
		}
	}
	.loading-text {
		// width: 700px;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 60px;
		line-height: 60px;
		color: #979797;
		font-size: 24px;
	}
</style>
